#include<bits/stdc++.h> 

using namespace std;
using  ll  =long long;
void solve()
{
	//3 7

	
	ll p,k;
	cin>> p >> k;
	ll ans=0;
	if(k<=p*2)
	{
		cout<<p<<endl;
	}
	else {
		ll now=0;
		ll s=1;
		while(now+s<=k)
		{
			now+=s;
			s*=2;
		}
		cout<<s/2+(k-now)<<endl;
	}
}

int main( )
{
	ll t;
	cin >> t;
	while(t--)
	{
		solve();
	}
	return 0;
}
